<template>
  <div>
    <!--    面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
<!--    卡片视图区域-->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
<!--      表格数据区域-->
      <el-table
        :data="goodsListData"
        border
        style="width: 100%" stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="600">
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)">
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量">
        </el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="delGoods(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
<!--      分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <!--      添加商品的 对话框-->
      <!--      编辑商品的 对话框-->
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'List',
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      goodsListData: [],
      total: 0,
      editGoodsForm: {
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: ''
      }
    }
  },
  methods: {
    // 获取商品列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表数据失败！')
      }
      // console.log(res)
      this.$message.success('获取商品列表数据成功！')
      this.goodsListData = res.data.goods
      this.total = res.data.total
      // console.log(this.goodsList)
      // this.queryInfo.query = ''
    },
    handleSizeChange (size) {
      // console.log(size)
      this.queryInfo.pagesize = size
      this.getGoodsList()
    },
    handleCurrentChange (page) {
      // console.log(page)
      this.queryInfo.pagenum = page
      this.getGoodsList()
    },
    // 删除商品
    delGoods (id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // console.log(id)
        const { data: res } = await this.$http.delete('goods/' + id)
        if (res.meta.status !== 200) {
          return this.$message.error('删除商品失败！')
        }
        this.$message.success('删除商品成功！')
        await this.getGoodsList()
      }).catch(err => {
        console.log(err)
        this.$message.info('取消删除操作！')
      })
    },
    showEditDialog (info) {
      this.editGoodsForm.goods_name = info.goods_name
      this.editGoodsForm.goods_price = info.goods_price
      this.editGoodsForm.goods_number = info.goods_number
      this.editGoodsForm.goods_weight = info.goods_weight
      // console.log(this.editGoodsForm)
    },
    goAddPage () {
      this.$router.push('/goods/add')
    }
  },
  created () {
    // 获取商品列表数据
    this.getGoodsList()
  }
}
</script>

<style lang="less" scoped>
  .el-col {
    margin-right: 15px;
  }
</style>
